package com.compus_help.service.impl;

import com.compus_help.Mapper.AdminMapper;
import com.compus_help.Mapper.adminDashboardMapper;
import com.compus_help.service.adminDashboardService;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class adminDashboardServiceImpl implements adminDashboardService {

    @Autowired
    private adminDashboardMapper adminDashboardMapper;

    // adminDashboardServiceImpl.java
    @Override
    public Map<String, Object> getDashboardStats() {
        // 查询学生和订单总数
        Integer studentsTotal = adminDashboardMapper.getStudentsTotal();
        Integer ordersTotal = adminDashboardMapper.getOrdersTotal();

        // 查询各个分类的订单数
        List<Map<String, Integer>> categoryOrdersTotal = adminDashboardMapper.getCategoryOrdersTotal();
        List<Map<String, Object>> convertedCategoryOrdersTotal = new ArrayList<>();

        for (Map<String, Integer> map : categoryOrdersTotal) {
            Map<String, Object> convertedMap = new HashMap<>();
            for (Map.Entry<String, Integer> entry : map.entrySet()) {
                convertedMap.put(entry.getKey(), entry.getValue());
            }
            convertedCategoryOrdersTotal.add(convertedMap);
        }

        // 设置返回结果
        Map<String, Object> result = Map.of(
                "studentsTotal", studentsTotal,
                "ordersTotal", ordersTotal,
                "categoryOrdersTotal", convertedCategoryOrdersTotal
        );

        return result;
    }

    @Override
    public List<Map<String, Object>> getStudentDistributionByCollege() {
        // 查询学生按学院分布
        List<Map<String, Object>> studentDistributionByCollege = adminDashboardMapper.getStudentDistributionByCollege();
        //设置返回结果
        return studentDistributionByCollege;
    }

    @Override
    public List<Map<String, String>> getCollegeList() {
        //查询学生类别中的学院类别
        List<Map<String, String>> collegeList = adminDashboardMapper.getCollegeList();
        return collegeList;

    }

    @Override
    public byte[] exportStudentsReport(String[] colleges) {
        try {
            // 调用Mapper方法获取指定学院的学生数据
            List<Map<String, Object>> studentData = adminDashboardMapper.getStudentsByColleges(List.of(colleges));

            // 创建Excel工作簿
            Workbook workbook = new XSSFWorkbook();
            // 动态拼接学院名字
            StringBuilder collegeNameBuilder = new StringBuilder();
            for (int i = 0; i < colleges.length; i++) {
                collegeNameBuilder.append(colleges[i]);
                if (i != colleges.length - 1) {
                    collegeNameBuilder.append("、");
                }
            }
            Sheet sheet = workbook.createSheet(collegeNameBuilder.toString());

            // 创建表头
            Row headerRow = sheet.createRow(0);
            headerRow.createCell(0).setCellValue("学号");
            headerRow.createCell(1).setCellValue("姓名");
            headerRow.createCell(2).setCellValue("学院");
            headerRow.createCell(3).setCellValue("班级"); // 注意此处原代码列索引错误，已修正
            headerRow.createCell(4).setCellValue("联系方式");

            // 填充数据
            int rowNum = 1;
            for (Map<String, Object> student : studentData) {
                Row row = sheet.createRow(rowNum++);
                row.createCell(0).setCellValue((String) student.get("campus_card_number"));
                row.createCell(1).setCellValue((String) student.get("student_name"));
                row.createCell(2).setCellValue((String) student.get("college"));
                row.createCell(3).setCellValue((String) student.get("class_name"));
                row.createCell(4).setCellValue((String) student.get("phone"));
            }

            // 自动调整列宽
            for (int i = 0; i < 5; i++) {
                sheet.autoSizeColumn(i);
            }

            // 将工作簿写入字节数组
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            workbook.write(outputStream);
            workbook.close();

            return outputStream.toByteArray();
        } catch (IOException e) {
            throw new RuntimeException("导出学生报表失败", e);
        }
    }

}



